/**
 * @Description:
 * @author 若琳
 * @date 2021/7/20
*/


const {MYSQL}  = require("../../../../../../../../ruoling-common/src/com/ruoling/common/config/mysqlconfig.js");
const Pager_DB = require("../../../../../../../../ruoling-common/src/com/ruoling/common/table/Pager_DB.js");

module.exports = class RoleService{


    /**
     * 角色菜单复选框Service
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async RoleMenuCheckTreeService(callback){
        let connect = null;
        try{
            connect = await MYSQL().connect();
            let obj = {
                sql:"SELECT ID as id,sys_parent_id as parentId, sys_menu_name as title, \"0\" as checkArr FROM rl_sys_menu_permisson WHERE Is_delete = 1 and sys_available = 1"
            };

            let data = await connect.connect_query_close(obj.sql);
            callback(null,data);
            return ;

        }catch (e){
            if(connect != null){
                connect.close();
            }
            callback(e);
            return ;
        }
    }

    /**
     * 角色添加Service
     * @param sys_role_name
     * @param sys_role_sort
     * @param sys_role_state
     * @param arr_Id
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async RoleAddService(sys_role_name,sys_role_sort,sys_role_state,arr_Id,callback){
        let startTrans = null;
        try{
            let connect = await MYSQL().connect();
            startTrans = await connect.startTrans();
            let role_obj = {
                sql:"insert into rl_sys_role(sys_role_name,sys_role_sort,sys_role_state,Create_time)values(?,?,?,?)",
                param:[sys_role_name,sys_role_sort,sys_role_state,new Date()]
            };

            let roleId = await startTrans.query(role_obj.sql,role_obj.param);

            let obj = {
                sql:"insert into rl_sys_role_menu(sys_role_id,sys_menu_id,Create_time)values ?",
                param:[]
            };
            for(let i = 0;i<arr_Id.length;i++){
                let arr = [roleId.insertId,arr_Id[i],new Date()];
                obj.param.push(arr);
            }

            let sys_role_menu = await startTrans.query(obj.sql,[obj.param]);

            await startTrans.commit();
            callback(null,sys_role_menu.affectedRows);
            return;
        }catch (e){
            await startTrans.rollback();
            callback(e);
            return ;
        }

    };

    /**
     * 角色列表Service
     * @param limit
     * @param page
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async RoleListService(limit,page,callback){
        try{
            let pagerdb = await new Pager_DB(limit,page).table("rl_sys_role").select();

                callback(null,pagerdb);
                return;
        }catch (e){
           callback(e);
           return;
        }
    };

    /**
     *
     * @param Id
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async RoleByIdEditDataService(Id,callback){
        let connect = null;
        try{
            connect = await MYSQL().connect();
            let obj = {
                sql:"select * from rl_sys_role where ID="+Id+" and Is_delete = 1",
            };

            let role_menu_Id = {
                sql:"SELECT sys_menu_id FROM rl_sys_role_menu WHERE sys_role_id = "+Id+" and Is_delete = 1",
            }
            let role_data  = await connect.connect_query(obj.sql);
            let menu_data_id = await  connect.connect_query_close(role_menu_Id.sql);
            let menu_arr = [];

            for(let i = 0;i<menu_data_id.length;i++){
                menu_arr.push(menu_data_id[i].sys_menu_id)
            }

            let list = {
                role_data:role_data[0],
                menu_data_id:menu_arr
            }

            callback(null,list);
            return;

        }catch (e){

            if(connect){
                connect.close();
            }
            callback(e);
            return;
        }
    };


    /**
     * 角色修改Service
     * @param sys_role_name
     * @param sys_role_sort
     * @param sys_role_state
     * @param arr_Id
     * @param ID
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async RoleEditServie(sys_role_name,sys_role_sort,sys_role_state,arr_Id,ID,callback){
        let startTrans = null;
        try{
            let connect = await MYSQL().connect();
            startTrans = await connect.startTrans();
            let role_obj = {
                sql:"UPDATE rl_sys_role SET sys_role_name = \""+sys_role_name+"\",sys_role_state = "+sys_role_state+",sys_role_sort = "+sys_role_sort+" WHERE ID = "+ID+"",
            };
            await startTrans.query(role_obj.sql);
            let role_menu_obj = {
                sql:"UPDATE rl_sys_role_menu SET Is_delete = 0 WHERE sys_role_id = "+ID+""
            };
            await startTrans.query(role_menu_obj.sql);
            let role_menu_insert_obj = {
                sql:"insert into rl_sys_role_menu(sys_role_id,sys_menu_id,Create_time)values ?",
                param:[]
            };
            for(let i = 0;i<arr_Id.length;i++){
                let arr = [ID,arr_Id[i],new Date()];
                role_menu_insert_obj.param.push(arr);
            }

            let sys_role_menu = await startTrans.query(role_menu_insert_obj.sql,[role_menu_insert_obj.param]);

            await startTrans.commit();
            callback(null,sys_role_menu.affectedRows);
            return;

        }catch (e){
            await startTrans.rollback();
            callback(e);
            return ;
        }
    };

    /**
     * 获取角色渲染用户
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async RoleCheckarrServie(callback){
        let connect = null;
        try{
            connect = await MYSQL().connect();
            let obj = {
                sql:"select * from rl_sys_role where Is_delete = 1 and sys_role_state = 1"
            }
            let list = await connect.connect_query_close(obj.sql);
            callback(null,list);
            return;
        }catch (e){
            if(connect){
                connect.close();
            }
            callback(e);
            return;
        }
    }

}